import Vuex from "vuex";
import Vue from "vue";
Vue.use(Vuex);

const store = new Vuex.Store({
    state: {
        useCount: 0,
    },
    getters: {
        getUseCount(state) {
            return state.useCount;
        },
    },
    mutations: {
        initCount(state) {
            let before = uni.getStorageSync("use_count");
			console.log(before);
            if (before == 1 || before.length == 0) {
                if (before == 1) {
                    uni.clearStorageSync("use_count");
                }
                if (before.length == 0) {
                    before = uni.getStorageSync("after_count");
                }
            } else {
                uni.setStorageSync("after_count", before);
                uni.clearStorageSync("use_count");
            }
            // before = uni.getStorageSync("after_count");
            if (before.length !== 0) {
                state.useCount = before * 1;
            }
        },
        updateCount(state, num) {
            state.useCount += num;
            uni.setStorage({
                key: "after_count",
                data: state.useCount,
                success: () => {
                    uni.showToast({
                        title: "数据更新成功",
                        duration: 1000,
                    });
                },
            });
        },
    },
});

export default store;
